package com.mcuking.mwbpcontainer.application;

import android.content.Context;
import android.app.Application;
import android.util.Log;

import com.hht.webpackagekit.PackageManager;
import com.hht.webpackagekit.core.Constants;
import com.hht.webpackagekit.core.PackageEntity;
import com.hht.webpackagekit.core.PackageInfo;
import com.hht.webpackagekit.core.PackageStatus;
import com.hht.webpackagekit.core.util.FileUtils;
import com.hht.webpackagekit.core.util.GsonUtils;
import com.hht.webpackagekit.core.util.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MWBPApplication extends Application {

    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        EventBus.getDefault().register(this);

        PackageManager.getInstance().init(context);
        getPackageIndex(Constants.BASE_PACKAGE_INDEX);
    }

    private void getPackageIndex(final String url){
        PackageEntity packageIndexBean = new PackageEntity();
        List<PackageInfo> arrayList = new ArrayList<>();
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.setPackageId("main");
        packageInfo.setVersion("2");
        packageInfo.setStatus(PackageStatus.onLine);

        //如果客户端和服务器版本相差>1 (是 全量包), 那么设置这个, path 是相对域名的路径
        packageInfo.setOrigin_file_path("/package.zip");
        packageInfo.setOrigin_file_md5("d8b9af9a83e50f8697e8967d67f3ed21");

        //如果客户端和服务器版本相差=1 (是 差分包), 那么设置这个
        //http://192.168.0.2:8080/patch_pkg/patch.zip
        packageInfo.setPatch_file_path("/patch_pkg/patch.zip");
        packageInfo.setPatch_file_md5("165470ddb937be8209bfab57fa5fc20e");

        arrayList.add(packageInfo);
        packageIndexBean.setItems(arrayList);

        new Thread(new Runnable() {
            @Override
            public void run() {
//                try {
//                    OkHttpClient client = new OkHttpClient();
//                    Request request = new Request.Builder().url(url).build();
//                    try (Response response = client.newCall(request).execute()) {
//                        String data = response.body().string();
//                        Logger.d( "application getPackageIndex  do post, response data = " + data);
//                        EventBus.getDefault().post(data); //从服务端获取最新的packageIndex.json
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                downloadAndSaveIndexHtml();

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                String resultStr = GsonUtils.toJsonStr(packageIndexBean);
                EventBus.getDefault().post(resultStr);
            }
        }).start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updatePackageManager(String res){
        Log.d("getPackageIndex", "updatePackageManager");
        PackageManager.getInstance().update(res);
    }

    public static Context getGlobalContext() {
        return context;
    }
}
